#!/usr/bin/python
#coding: utf-8
import os,sys

""" 查找所有给定路径下的文件和文件夹.  2011.7.25 23:20 星期一

以下是所有原始代码,为正确运行后,将其封装成类 class YSeach...
basepath=os.path.abspath(sys.argv[0])
dirpath=os.path.dirname(basepath)
dirs=[]
dirs.append(dirpath)
print dirs
print '---'*5
count=0
for sechdir in dirs:    
    #print sechdir
    print count,sechdir
    count+=1
    for i in os.listdir(sechdir):
        isdirpath=os.path.join(sechdir,i)
        if os.path.isdir(isdirpath):
            dirs.append(isdirpath)
            print dirs
            
        else:
            print isdirpath,'is not dir'"""



#将上面的原始代码封装成函数，用到了list.pop()从末尾弹出一个元素并删除这个元素
#该方法可以减少内存使用......
def index(directory='.'):
    # like os.listdir, but traverses directory trees
    if directory=='.':
        basepath=os.path.abspath(sys.argv[0])
        dirpath=os.path.dirname(basepath)
    else:
        basepath=os.path.abspath(directory)            
        dirpath=os.path.dirname(basepath)
    stack = [dirpath]
    files = []
    while stack:
        directory = stack.pop()
        for file in os.listdir(directory):
            fullname = os.path.join(directory, file)
            files.append(fullname)
            if os.path.isdir(fullname) and not os.path.islink(fullname):
                stack.append(fullname)
    return files

    
class YSeach(object):
    """seach dirpath all file.
    
    """
    def __init__(self,dir='.',echo=False):            
        if dir=='.':
            basepath=os.path.abspath(sys.argv[0])
            dirpath=os.path.dirname(basepath)
        else:
            basepath=os.path.abspath(dir)            
            dirpath=os.path.dirname(basepath)
        print "basepath===>",basepath
        print "dirpath===>",dirpath
        if echo==True:
            self.loging=os.path.join(dirpath,'logfile.txt')
            print self.loging
            logstdout=open(self.loging,'w')
            sys.stdout=logstdout
        else:
            self.loging=None
            
            
        self.dirs=[]
        self.dirs.append(dirpath)
        
        self.__dircount=0
        self.__filecount=0
        self.count=0
        print "hello,my test!"
        logstdout.close()
    def seach(self):        
        if self.loging:
            logstdout=open(self.loging,'a')
            sys.stdout=logstdout        
        self.count=0
        self.__dircount=0
        self.__filecount=0
        for sechdir in self.dirs:    
            print self.count,sechdir            
            try:
                for i in os.listdir(sechdir):
                    isdirpath=os.path.join(sechdir,i)
                    if os.path.isdir(isdirpath):
                        self.dirs.append(isdirpath)
                        #print self.dirs
                        self.__dircount+=1
                        
                    else:
                        #print isdirpath,'is not dir'
                        self.__filecount+=1
                self.count+=1
            except:
                pass
        logstdout.close()
    def getdircount(self):
        return self.__dircount
    def getfilecount(self):
        return self.__filecount

    